<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of employe
 *
 * @author Gmax
 */
class modules_societe_employe extends modele_user {
    private $_societe_id = 0;
    private $_societe = null;
    private $_post = '';
    
    public function __construct($id = null,$societe_id=0) {
        parent::__construct($id);
        $this->_societe_id = $societe_id;
        if($this->getId()>0&&$societe_id>0){
            $sqlQuery = 'SELECT post FROM '.PWE::getPrefixBdd().'user_societe WHERE user_id='.$this->getId().' AND societe_id='.$societe_id;
            $datas = $this->_getOneResult($sqlQuery);
            if($datas){
                $this->_post = $datas['post'];
            }
        }
    }
    /**
     * renvoi l'identifiant de societe ou 0
     * @return int
     */
    public function getSocieteId(){
        return $this->_societe_id;
    }
    /**
     * renvoi la société
     * @return /modules_societe_societe
     */
    public function getSociete(){
        if($this->_societe===null && $this->getSocieteId()>0){
            $this->_societe = new modules_societe_societe($this->getSocieteId());
        }
        return $this->_societe;
    }
    /**
     * renvoi le post occupé par l'employé
     * @param boolean $input
     * @return string
     */
    public function getPost($input=false){
        if($input)return strtolower($this->inputFormat($this->_post));
        else return ucfirst($this->outputFormat($this->_post));
    }
    
    public function setDatas($datas) {
        parent::setDatas($datas);
        if(isset($datas['_post'])){
            $this->_post = strtolower($datas['_post']);
        }elseif(isset($datas['post'])){
            $this->_post = strtolower($datas['post']);
        }
    }
    
    public function getDatas($input = false){
        $datas = parent::getDatas($input);
        $datas['post'] = $this->getPost($input);
        return $datas;
    }
    
    /* methodes statiques */
    
    /**
     * renvoi a liste des posts en base
     * @param boolean $decompte
     * @return array [count,value]|[value]
     */
    public static function getListePosts($decompte = false){
        $sqlQuery = 'SELECT COUNT(post) AS nbr, post AS post FROM '.PWE::getPrefixBdd().'user_societe GROUP BY post ORDER BY post';
        $results = bdd_sql::doSql($sqlQuery);
        $retour = array();
        if($results['count']>0){
            foreach ($results['results'] as $datas){
                if($decompte){
                    $retour[] = array('count'=>$datas['nbr'],'value'=>$datas['post']);
                }else{
                    $retour[] = $datas['post'];
                }
            }
        }
        
        return $retour;
    }
}